<template>
  <div class="user-profile-container">
    <el-row>
      <el-col :span="6" class="nav-column">
        <el-menu default-active="1" class="user-nav">
          <el-menu-item index="1" @click="activeTab = 'userInfo'">用户信息</el-menu-item>
          <el-menu-item index="2" @click="activeTab = 'accountSecurity'">账号安全</el-menu-item>
        </el-menu>
      </el-col>
      <el-col :span="18" class="content-column">
        <el-card v-if="activeTab === 'userInfo'">
          <h2>用户信息</h2>
          <el-form :model="form" ref="form" @submit.prevent="handleUpdate">
            <el-form-item label="用户名" prop="username" :rules="[{ required: true, message: '请输入用户名', trigger: 'blur' }]">
              <el-input v-model="form.username" placeholder="请输入用户名" class="input-field"></el-input>
            </el-form-item>

            <el-form-item label="电子邮件" prop="email" :rules="[{ required: true, message: '请输入电子邮件', trigger: 'blur' }, { type: 'email', message: '请输入有效的电子邮件地址' }]">
              <el-input v-model="form.email" placeholder="请输入电子邮件" class="input-field"></el-input>
            </el-form-item>

            <el-form-item label="手机号码" prop="phone" :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' }]">
              <el-input v-model="form.phone" placeholder="请输入手机号码" class="input-field"></el-input>
            </el-form-item>

            <el-form-item label="头像">
              <el-upload
                class="avatar-uploader"
                action="#"
                :show-file-list="false"
                :before-upload="beforeUpload"
                @change="handleAvatarChange"
              >
                <img v-if="form.avatar" :src="form.avatar" class="avatar" />
                <el-button size="small" type="primary">选择头像</el-button>
              </el-upload>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleUpdate">更新信息</el-button>
            </el-form-item>
          </el-form>
        </el-card>

        <el-card v-if="activeTab === 'accountSecurity'">
          <h2>账号安全</h2>
          <el-form :model="securityForm" ref="securityForm" @submit.prevent="handleSecurityUpdate">
            <el-form-item label="手机号" prop="phone" :rules="[{ required: true, message: '请输入手机号码', trigger: 'blur' }]">
              <el-input v-model="securityForm.phone" placeholder="请输入手机号码" class="input-field"></el-input>
            </el-form-item>

            <el-form-item label="新密码" prop="newPassword" :rules="[{ required: true, message: '请输入新密码', trigger: 'blur' }]">
              <el-input v-model="securityForm.newPassword" type="password" placeholder="请输入新密码" class="input-field"></el-input>
            </el-form-item>

            <el-form-item label="确认密码" prop="confirmPassword" :rules="[{ required: true, message: '请确认密码', trigger: 'blur' }]">
              <el-input v-model="securityForm.confirmPassword" type="password" placeholder="请确认密码" class="input-field"></el-input>
            </el-form-item>

            <el-form-item>
              <el-button type="primary" @click="handleSecurityUpdate">更新安全信息</el-button>
            </el-form-item>
          </el-form>
        </el-card>
      </el-col>
    </el-row>
  </div>
</template>

<script>
export default {
  name: 'UserProfile',
  data() {
    return {
      activeTab: 'userInfo', // 当前激活的标签
      form: {
        username: '',
        email: '',
        phone: '',
        avatar: ''
      },
      securityForm: {
        phone: '',
        newPassword: '',
        confirmPassword: ''
      }
    };
  },
  methods: {
    handleUpdate() {
      // 这里可以添加更新用户信息的逻辑，例如调用 API
      this.$message.success('用户信息更新成功！');
    },
    handleSecurityUpdate() {
      // 这里可以添加更新安全信息的逻辑，例如调用 API
      if (this.securityForm.newPassword !== this.securityForm.confirmPassword) {
        this.$message.error('两次输入的密码不一致');
        return;
      }
      this.$message.success('安全信息更新成功！');
    },
    beforeUpload(file) {
      const isImage = file.type.startsWith('image/');
      if (!isImage) {
        this.$message.error('上传头像只能是图片文件！');
      }
      return isImage;
    },
    handleAvatarChange(file) {
      const reader = new FileReader();
      reader.onload = (e) => {
        this.form.avatar = e.target.result; // 设置头像预览
      };
      reader.readAsDataURL(file.raw);
    }
  }
}
</script>

<style scoped>
.user-profile-container {
  max-width: 800px;
  margin: 100px auto;
}

.avatar {
  width: 100px;
  height: 100px;
  border-radius: 50%;
  margin-bottom: 10px;
}

.nav-column {
  border-right: 1px solid #e6e6e6;
}

.content-column {
  padding: 20px;
}

.input-field {
  width: 100%; /* 设置输入框宽度一致 */
}

.el-form-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
}

.el-form-item label {
  width: 100px; /* 设置标签宽度 */
  text-align: right; /* 标签右对齐 */
  margin-right: 10px; /* 标签与输入框之间的间距 */
}
</style> 